package com.ilearn.spring.future.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ilearn.spring.future.entity.User;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Project: cmcchq
 * @Description:
 * @Version 1.0
 * @Throws :
 * @Author: <li>2019/6/26 lyndon Create 1.0
 * @Copyright (c)2019-2019 版权所有
 * @Modified By:
 */
public class ResponseUtil {

    public static HttpServletResponse convertToHttp(HttpServletResponse response, User user) {
        response.setStatus(HttpStatus.OK.value());

        /*for (Map.Entry<String, List<String>> entry : odResponse.getAllHeaders().entrySet()) {
            for (String headerValue : entry.getValue()) {
                response.addHeader(entry.getKey(), headerValue);
            }
        }*/
        if (user != null) {
            try {
                String json = new ObjectMapper().writeValueAsString(user);
                response.getWriter().write(json);
                response.flushBuffer();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }
        return null;
    }
}
